package com.ymatou.operate.trading.api.testcase;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.operate.trading.api.parameter.ApplyValidationBean;
import com.ymatou.operate.trading.api.service.ApplyValidationCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.common.enums.CancelOrderTypeEnum;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.constant.ECode;

/**
 * 退货退款申请效验接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ApplyValidation {
    private static ApplyValidationBean applyvalidationBean;
    private static ApplyValidationCall applyvalidationCall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("退货退款申请校验接口");
    }

    @Before
    public void caseUp() {
        applyvalidationBean = new ApplyValidationBean();
        applyvalidationCall = new ApplyValidationCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(applyvalidationCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ApplyValidation_001() {
        Logger.start(true, "用户收货之后申请校验");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int userId =placeOrderResp.getUserId();         
            int tradingId = placeOrderResp.getTradingId();

            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
            // 支付订单
            new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);
            // 接单
            new OrderCallService().acceptOrder(orderId, sellerId);
            // 商家发货
            new OrderCallService().dispatched(orderId, sellerId);
            // 确认收货
            new OrderCallService().confirmReceive(userId, orderId);
            Logger.comment("前置数据准备结束");
            applyvalidationBean.setOrderId(orderId);
            applyvalidationCall.setData(applyvalidationBean);
            applyvalidationCall.callService();
            Logger.verifyEquals(ECode.SUCCESS, applyvalidationCall.getString("Code"), "验证返回码");
            // Logger.verifyEquals(102, applyvalidationCall.getOperateType(), "验证已收货可以申请售后操作");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ApplyValidation_002() {
        Logger.start(true, "卖家接单后申请效验");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int userId =placeOrderResp.getUserId();         
            int tradingId = placeOrderResp.getTradingId();

            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
            // 支付订单
            new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);
            // 接单
            new OrderCallService().acceptOrder(orderId, sellerId);
            Logger.comment("前置数据准备结束");
            applyvalidationBean.setOrderId(orderId);
            applyvalidationCall.setData(applyvalidationBean);
            applyvalidationCall.callService();
            Logger.verifyEquals(ECode.SUCCESS, applyvalidationCall.getString("Code"), "验证返回码");
            Logger.verifyEquals(true, applyvalidationCall.getData().getBoolean("IsValidationPass"),
                    "验证isValidationPass");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ApplyValidation_003() {
        Logger.start(true, "用户下单后申请效验");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int userId =placeOrderResp.getUserId();         
            int tradingId = placeOrderResp.getTradingId();

            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
            /*
             * // 支付订单 new OrderCallService().orderPayRequest(userId, totalPrice, tradingId); // 接单
             * new OrderCallService().acceptOrder(orderId, sellerId); // 商家发货 new
             * OrderCallService().dispatched(orderId, sellerId); // 确认收货 new
             * OrderCallService().confirmReceive(userId, orderId);
             */
            Logger.comment("前置数据准备结束");
            applyvalidationBean.setOrderId(orderId);
            applyvalidationCall.setData(applyvalidationBean);
            applyvalidationCall.callService();
            Logger.verifyEquals(ECode.SUCCESS, applyvalidationCall.getString("Code"), "验证返回码");
            // Logger.verifyEquals(11, applyvalidationCall.getOperateType(), "验证用户下单后可以申请取消订单操作");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ApplyValidation_004() {
        Logger.start(true, "用户支付后接单前效验成功");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int userId =placeOrderResp.getUserId();         
            int tradingId = placeOrderResp.getTradingId();

            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
            // 支付订单
            new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);
            /*
             * // 接单 new OrderCallService().acceptOrder(orderId, sellerId); // 商家发货 new
             * OrderCallService().dispatched(orderId, sellerId); // 确认收货 new
             * OrderCallService().confirmReceive(userId, orderId);
             */
            Logger.comment("前置数据准备结束");
            applyvalidationBean.setOrderId(orderId);
            applyvalidationCall.setData(applyvalidationBean);
            applyvalidationCall.callService();
            Logger.verifyEquals(ECode.SUCCESS, applyvalidationCall.getString("Code"), "验证返回码");
            // Logger.verifyEquals(11, applyvalidationCall.getOperateType(), "验证用户支付可以申请取消订单操作");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ApplyValidation_005() {
        Logger.start(true, "商家接单前取消订单,效验成功");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int userId =placeOrderResp.getUserId();         
            int tradingId = placeOrderResp.getTradingId();

            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
            // 支付订单
            new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);
            // 接单
            new OrderCallService().acceptOrder(orderId, sellerId);
            // 取消订单
            new OrderCallService().cancelOrder(orderId, userId, CancelOrderTypeEnum.CustomerCancelOrder);
            Logger.comment("前置数据准备结束");
            applyvalidationBean.setOrderId(orderId);
            applyvalidationCall.setData(applyvalidationBean);
            applyvalidationCall.callService();
            Logger.verifyEquals(ECode.FAILED201, applyvalidationCall.getString("Code"), "验证返回码");
            Logger.verifyEquals("订单已取消，不能退货退款：" + applyvalidationBean.getOrderId(),
                    applyvalidationCall.getString("Msg"), "验证返回码");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ApplyValidation_006() {
        Logger.start(true, "商家接单后取消订单,效验成功");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int userId =placeOrderResp.getUserId();         
            int tradingId = placeOrderResp.getTradingId();

            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
            // 支付订单
            new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);
            // 接单
            new OrderCallService().acceptOrder(orderId, sellerId);
            // 取消订单
            new OrderCallService().cancelOrder(orderId, userId);
            Logger.comment("前置数据准备结束");
            applyvalidationBean.setOrderId(orderId);
            applyvalidationCall.setData(applyvalidationBean);
            applyvalidationCall.callService();
            Logger.verifyEquals(ECode.SUCCESS, applyvalidationCall.getString("Code"), "验证返回码");
            Logger.verifyEquals(true, applyvalidationCall.getData().getBoolean("IsValidationPass"),
                    "验证商家接单后取消订单没有可以操作动作");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P3.class)
    @TestCase
    public void Tc_ApplyValidation_007() {
        Logger.start(false, "订单号不存在申请效验");
        try {
            applyvalidationBean.setOrderId(111111);
            applyvalidationCall.setData(applyvalidationBean);
            applyvalidationCall.callService();
            Logger.verifyEquals(ECode.FAILED201, applyvalidationCall.getString("Code"), "验证返回码");
            Logger.verifyEquals("订单不存在:" + applyvalidationBean.getOrderId(),
                    applyvalidationCall.getString("Msg"), "验证错误提示");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P3.class)
    @TestCase
    public void Tc_ApplyValidation_008() {
        Logger.start(false, "订单号为null申请效验");
        try {
            // applyvalidationBean.setOrderId(111111);
            applyvalidationCall.setData(applyvalidationBean);
            applyvalidationCall.callService();

            Logger.verifyEquals(ECode.FAILED400, applyvalidationCall.getString("Code"), "验证返回码");
            Logger.verifyEquals(ECode.ERROR400_MSG_9, applyvalidationCall.getString("Msg"), "验证错误提示");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P3.class)
    @TestCase
    public void Tc_ApplyValidation_009() {
        Logger.start(false, "订单退货退款中");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int userId =placeOrderResp.getUserId();         
            int tradingId = placeOrderResp.getTradingId();

            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
            // 支付订单
            new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);
            // 退货退款同步中
            new OrderCallService().applySalesRefundRequestInJava(orderId);
            applyvalidationBean.setOrderId(orderId);
            applyvalidationCall.setData(applyvalidationBean);
            applyvalidationCall.callService();
            Logger.verifyEquals(ECode.SUCCESS, applyvalidationCall.getString("Code"), "验证返回码");
            Logger.verifyEquals(true, applyvalidationCall.getData().getBoolean("IsValidationPass"),
                    "验证退货退款中的订单不能操作退货退款");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ApplyValidation_010() {
        Logger.start(true, "商家接单后,退货退款校验");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            OrderCallService orderCallService=new OrderCallService();
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int userId =placeOrderResp.getUserId();         
            int tradingId = placeOrderResp.getTradingId();

            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
            // 付款
            orderCallService.orderPayRequest(userId, totalPrice, tradingId);
            // 接单
            orderCallService.acceptOrder(orderId, sellerId);
            Logger.comment("前置数据准备结束");
            applyvalidationBean.setOrderId(orderId);
            applyvalidationBean.setRefundAmount(totalPrice);
            applyvalidationCall.setData(applyvalidationBean);
            applyvalidationCall.callService();
            Logger.verifyEquals(ECode.SUCCESS, applyvalidationCall.getString("Code"), "验证返回码");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ApplyValidation_011() {
        Logger.start(true, "商家接单72小时后,退货退款校验");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            OrderCallService orderCallService = new OrderCallService();
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int userId =placeOrderResp.getUserId();         
            int tradingId = placeOrderResp.getTradingId();

            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
            // 付款
            orderCallService.orderPayRequest(userId, totalPrice, tradingId);
            // 接单
            orderCallService.acceptOrder(orderId, sellerId);
            // 修改接单时间为72小时前
            new DefaultWapper().updateOrderAcceptTimeByOrderId(orderId);
            Logger.comment("前置数据准备结束");
            applyvalidationBean.setOrderId(orderId);
            applyvalidationBean.setRefundAmount(totalPrice);
            applyvalidationCall.setData(applyvalidationBean);
            applyvalidationCall.callService();
            Logger.verifyEquals(ECode.SUCCESS, applyvalidationCall.getString("Code"), "验证返回码");
            Logger.verifyEquals(true, applyvalidationCall.getData().getBoolean("IsValidationPass"),
                    "验证IsValidationPass");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ApplyValidation_012() {
        Logger.start(true, "商家发货后,退货退款校验");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            OrderCallService orderCallService = new OrderCallService();
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int userId =placeOrderResp.getUserId();         
            int tradingId = placeOrderResp.getTradingId();

            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
            // 付款
            orderCallService.orderPayRequest(userId, totalPrice, tradingId);
            // 接单
            orderCallService.acceptOrder(orderId, orderId);
            orderCallService.dispatched(orderId, orderId);
            Logger.comment("前置数据准备结束");
            applyvalidationBean.setOrderId(orderId);
            applyvalidationBean.setRefundAmount(totalPrice);
            applyvalidationCall.setData(applyvalidationBean);
            applyvalidationCall.callService();
            Logger.verifyEquals(ECode.SUCCESS, applyvalidationCall.getString("Code"), "验证返回码");
            Logger.verifyEquals(true, applyvalidationCall.getData().getBoolean("IsValidationPass"),
                    "验证IsValidationPass");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ApplyValidation_013() {
        Logger.start(true, "商家发货10天后,退货退款校验");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            OrderCallService orderCallService = new OrderCallService();
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int userId =placeOrderResp.getUserId();         
            int tradingId = placeOrderResp.getTradingId();

            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
            // 付款
            orderCallService.orderPayRequest(userId, totalPrice, tradingId);
            // 接单
            orderCallService.acceptOrder(orderId, sellerId);
            orderCallService.dispatched(orderId, sellerId);
            new DefaultWapper().updateOrderDispathTimeByOrderId(orderId);
            Logger.comment("前置数据准备结束");
            applyvalidationBean.setOrderId(orderId);
            applyvalidationBean.setRefundAmount(totalPrice);
            applyvalidationCall.setData(applyvalidationBean);
            applyvalidationCall.callService();
            Logger.verifyEquals(ECode.SUCCESS, applyvalidationCall.getString("Code"), "验证返回码");
            Logger.verifyEquals(true, applyvalidationCall.getData().getBoolean("IsValidationPass"),
                    "验证IsValidationPass");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ApplyValidation_014() {
        Logger.start(true, "确认收货15天后,退货退款校验");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            OrderCallService orderCallService = new OrderCallService();
            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

            int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
            int userId =placeOrderResp.getUserId();         
            int tradingId = placeOrderResp.getTradingId();

            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
            // 付款
            orderCallService.orderPayRequest(userId, totalPrice, tradingId);
            // 接单
            orderCallService.acceptOrder(orderId, sellerId);
            orderCallService.dispatched(orderId, sellerId);
            orderCallService.confirmReceive(userId, orderId);
            new DefaultWapper().updateOrderConfirmedTimeByOrderId(orderId);
            Logger.comment("前置数据准备结束");
            applyvalidationBean.setOrderId(orderId);
            applyvalidationBean.setRefundAmount(totalPrice);
            applyvalidationCall.setData(applyvalidationBean);
            applyvalidationCall.callService();
            Logger.verifyEquals(ECode.SUCCESS, applyvalidationCall.getString("Code"), "验证返回码");
            Logger.verifyEquals(false, applyvalidationCall.getData().getBoolean("IsValidationPass"),
                    "验证IsValidationPass");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
}
